package com.qfedu.web;

import com.qfedu.pojo.Category;
import com.qfedu.pojo.Product;
import com.qfedu.service.CategoryService;
import com.qfedu.service.ProductService;
import com.qfedu.service.impl.CategoryServiceImpl;
import com.qfedu.service.impl.ProductServiceImpl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.function.Consumer;

//客户端入口
public class ProductApp {
    static ProductService productService = new ProductServiceImpl();
    static CategoryService categoryService = new CategoryServiceImpl();

    public static void main(String[] args) {
        // Scanner sc=new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("输入以下命令进行操作");
            System.out.println("C:创建 U:修改 D:删除 FA:查询所有 FI:根据id查询 DA:根据id批量删除 Q:退出");
            System.out.println("CC:创建商品分类 UC:修改商品分类 DC:删除商品分类 FAC:查询所有商品分类 FIC:根据id查询商品分类 DAC:根据id批量删除商品分类 Q:退出");
            try {
                String s = br.readLine();
                switch (s.toUpperCase()) {
                    case "C":
                        // System.out.println("创建");
                        createProduct(br);
                        break;
                    case "U":
                        // System.out.println("修改");
                        updateProduct(br);
                        break;
                    case "D":
                        // System.out.println("删除");
                        deleteProById(br);
                        break;
                    case "FA":
                        //  System.out.println("查询所有");
                        findAllProduct();
                        break;
                    case "FI":
                        // System.out.println("根据id查询");
                        findProById(br);
                        break;
                    case "DA":
                        // System.out.println("根据id批量删除");
                        deleteManyProById(br);
                        break;
                    case "Q":
                        System.out.println("Bye~");
                        System.exit(1);
                        break;
                    case "CC":
                        // System.out.println("创建商品分类");
                        creatCategory(br);
                        break;
                    case "UC":
                        // System.out.println("修改商品分类");
                        updateCategory(br);
                        break;
                    case "DC":
                       // System.out.println("删除商品分类");
                        deleteCateById(br);
                        break;
                    case "FAC":
                        // System.out.println("查询所有商品分类");
                        findAllCategory();
                        break;
                    case "FIC":
                        // System.out.println("根据id查询商品分类");
                        findCateById(br);
                        break;
                    case "DAC":
                       // System.out.println("根据id批量删除商品分类");
                        deleteManyCateById(br);
                        break;
                    default:
                        System.out.println("输入有误，请重试");
                        break;
                }
            } catch (Exception e) {
                System.out.println("系统正在维护，请稍后重试！");
            }
        }
    }

    private static void deleteManyCateById(BufferedReader br){
        List<String> list =new ArrayList<>();
        while (true){
            System.out.println("请输入你要删除的品类ID，按'y'退出");
            try {
               String cid= br.readLine();
               if ("y".equals(cid))break;
               else {
                   list.add(cid);
               }
            }catch (Exception e){
                System.out.println("输入有误，请重新输入");
            }
        }
      list.stream().forEach(cid-> categoryService.deleteCateById(cid));
        System.out.println("批量删除成功");
    }

    private static void deleteCateById(BufferedReader br) {
        try {
            System.out.println("请输入要删除的品类ID：");
            String cid = br.readLine();
            Category category = categoryService.fidCateById(cid);
            int rows = categoryService.deleteCateById(category.getCid());
            if (rows > 0) System.out.println("品类删除成功");
            else System.out.println("品类删除失败，请重新输入");
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }
    }


    private static void updateCategory(BufferedReader br) {
        try {
            System.out.println("请输入要修改的品类ID：");
            String cid = br.readLine();

            System.out.println("请输入新的品类ID");
            String newCid = br.readLine();
            System.out.println("请输入要修改的品类名称：");
            String cname = br.readLine();
            Category category = categoryService.fidCateById(cid);
            category.setCid(newCid);
            category.setCname(cname);
            int rows = categoryService.updateCategory(category, cid);
            if (rows > 0) System.out.println("品类修改成功");
            else System.out.println("品类修改失败，请重新输入");
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }

    }

    private static void creatCategory(BufferedReader br) {
        try {
            Category category = new Category();
            System.out.println("请输入品类id");
            String cid = br.readLine();
            System.out.println("请输入品类名称");
            String cname = br.readLine();
            category.setCid(cid);
            category.setCname(cname);
            int rows = categoryService.createCategory(category);
            if (rows > 0) System.out.println("品类创建成功");
            else System.out.println("品类创建失败，请重新输入");
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }
    }

    private static void findCateById(BufferedReader br) {
        System.out.println("请输入要查询的商品分类id:");
        try {
            String id = br.readLine();
            Category category = categoryService.fidCateById(id);
            System.out.println(category);
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }
    }

    private static void findAllCategory() {
        System.out.println("商品的所有分类如下所示：");
        List<Category> list = categoryService.findAllCategory();
        list.stream().forEach(category -> System.out.println(category));
    }

    private static void deleteManyProById(BufferedReader br) {
        List<Integer> list = new ArrayList<>();
        while (true) {
            System.out.println("请输入你要删除的商品ID，输入y结束");
            try {
                String id = br.readLine();
                if (id.equals("y")) break;
                else list.add(Integer.parseInt(id));

            } catch (Exception e) {
                System.out.println("输入有误，请重新输入");
            }
        }
        productService.deleteManyById(list);
        System.out.println("批量删除成功");
    }

    private static void deleteProById(BufferedReader br) {
        System.out.println("请输入你要删除的商品id:");
        try {
            int id = Integer.parseInt(br.readLine());
            Product product = productService.findProById(id);
            if (product != null) {
                int rows = productService.deleteProById(product);
                if (rows > 0) System.out.println("删除成功！");
                else System.out.println("删除失败，请重新删除");
            }
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }
    }

    private static void updateProduct(BufferedReader br) {
        //根据ID查询，然后修改
        System.out.println("请输入你要修改的商品id:");
        try {
            int id = Integer.parseInt(br.readLine());
            Product product = productService.findProById(id);
            //-----------------
            product = CUProduct(product, br);
            //-----------------
            int rows = productService.updateProduct(product);
            if (rows > 0) System.out.println("修改成功！");
            else System.out.println("修改失败，请重新修改");
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }
    }

    private static void createProduct(BufferedReader br) {
        Product product = new Product();
        //-----------------
        product = CUProduct(product, br);
        //-------------
        int rows = productService.addProduct(product);
        if (rows > 0) System.out.println("添加成功！");
        else System.out.println("添加失败，请重新添加");
    }

    public static Product CUProduct(Product product, BufferedReader br) {
        try {
            System.out.println("请输入商品信息");
            System.out.print("1、输入商品的名称：");
            String pname = br.readLine();
            System.out.print("请输入商品价格：");
            int price = Integer.parseInt(br.readLine());
            System.out.println("请选择商品是否上架：1 代表上架 0 代表下架");
            int flag = Integer.parseInt(br.readLine());
            System.out.println("请选择商品分类，输入编号");
            //获取商品的所有分类并选择赋值
            List<Category> list = categoryService.findAllCategory();
            Map<String, String> map = new HashMap<>();
            list.stream().forEach(category -> {
                System.out.println("商品ID：" + category.getCid() + "\t商品分类：" + category.getCname());
                map.put(category.getCid(), category.getCname());
            });
            String cid = br.readLine();
            product.setPname(pname);
            product.setPrice(price);
            product.setFlag(flag);
            product.setCid(cid);
            return product;
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }
        return null;
    }

    public static void findProById(BufferedReader br) {
        System.out.println("请输入id");
        try {
            int id = Integer.parseInt(br.readLine());
            Product product = productService.findProById(id);
            System.out.println(product);
        } catch (Exception e) {
            System.out.println("输入有误，请重新输入");
        }
    }

    public static void findAllProduct() {
        List<Product> list = productService.findAll();
        list.stream().forEach(product -> System.out.println(product));
    }
}
